<?php

namespace iBrand\Shop\Backend\Repository\Eloquent;

use iBrand\Component\Goods\Repositories\GoodsRepository;
use iBrand\Store\Backend\Model\Goods;
use Prettus\Repository\Eloquent\BaseRepository;

class GoodsRepositoryEloquent extends BaseRepository implements GoodsRepository
{
	public function model()
	{
		return Goods::class;
	}

	public function getGoodsByGoodsNo($goods_no)
	{
		return $this->findWhere(['goods_no' => $goods_no, 'status' => 1])->first();
	}

	public function getGoodsByBarCode($bar_code)
	{
		return $this->findWhere(['goods_barcode' => $bar_code, 'status' => 1])->first();
	}

	public function getGoodsById($goods_id)
	{
		return $this->findWhere(['id' => $goods_id, 'status' => 0])->first();
	}

	public function searchGoodsPaginate($searchKey, $limit = 15)
	{
		$query = $this->scopeQuery(function ($query) use ($searchKey) {

			if ($searchKey) {
				$query = $query->where('name', 'like', '%' . $searchKey . '%')->orWhere('goods_no', 'like', '%' . $searchKey . '%');
			}

			return $query->orderBy('sort', 'DESC')->orderBy('created_at', 'DESC');
		});

		return $query->paginate($limit);
	}
}